package com.example.controller;

import com.example.dto.SysLoginDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

/**
 * @author jie
 * @date 2021/11/16 -21:39
 */
@Api("登录")
@RequestMapping("sys")
@RestController
public class SysLoginController {

    @GetMapping("login")
    public String login() {
        return "请登录";
    }

    @ApiOperation("登录")
    @PostMapping("doLogin")
    public String doLogin(@RequestBody SysLoginDto sysLoginDto) {
        String result = "";
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(sysLoginDto.getUsername(), sysLoginDto.getPassword()));
            result = "登录成功";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            result = "登录失败";
        }

        return result;
    }
}
